package cn.pn.project.controller;

import cn.pn.project.client.commodity.SkuFeign;
import cn.pn.project.client.schedule.CommoditySessionFeign;
import cn.pn.project.common.DtoUtil;
import cn.pn.project.pojo.commodity.Sku;
import cn.pn.project.pojo.schedule.Commoditysession;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.SessionCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/commoditysession")
public class CommoditysessionController {

    @Autowired
    private CommoditySessionFeign commoditySessionFeign;

    @Autowired
    private SkuFeign skuFeign;

    /**
     * 根据场次id查询查询场地id
     * @param sessionIds
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/p/getPlaceIdBySessionIds",method = RequestMethod.POST)
    public Object getCommoditysessionBySessionId(@RequestParam("sessionIds") String sessionIds) throws SQLException{
        if(sessionIds==""||sessionIds==null){
            return DtoUtil.returnSuccess("场次id为空","5004");
        }
        return DtoUtil.returnSuccess("获取成功",commoditySessionFeign.getCommoditysessionBySessionId(sessionIds));
    }


    /**
     * 查询场次信息，调用sku方法，根据商品id查询场次id，然后根据场次id查询场次具体信息
     * @param commodity
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/p/getCommoditysessionByPlaceId",method = RequestMethod.POST)
    public Object getCommoditySession(@RequestParam("commodity") Integer commodity) throws SQLException{
        System.out.println("===========================commdityId="+commodity);
        if(commodity==null){
            return DtoUtil.returnSuccess("商品id为空","5005");
        }
        List<Sku> skuList=skuFeign.getSkuByCommdityId(commodity);
        System.out.println(JSON.toJSONString(skuList,true));
        String sessionId="";
        for(int i=0;i<skuList.size();i++) {
            if (skuList.size() > 1) {
                sessionId += skuList.get(i).getSessionId() + (i < skuList.size() - 1 ? "," : "");
            } else {
                sessionId = skuList.get(i).getSessionId().toString();
            }
        }
        System.out.println("====================================sessionId="+sessionId);
        List<Commoditysession> commoditysessionList=(List<Commoditysession>)commoditySessionFeign.getCommoditysessionBySessionId(sessionId);
        System.out.println(JSON.toJSONString(commoditysessionList,true));
        return DtoUtil.returnSuccess("获取成功",commoditysessionList);
        //return "callback("+ JSON.toJSONString(commoditysessionList,true) +")";
    }
}
